<?php
class Admin{
    protected $main_table = 'sys_admin';
	protected $main_id = 'admin_id';
	protected $db = null;
	
	public function __construct($db){
		$this->db=$db;
	}
   
	public function __destruct(){
		unset($this->db);
	}
	public function get_table_name() {
	return $this->main_table;
    }
	/**
	* 	insert user
	* 	@param array $data 
	* 	@return int affected count
	*/
	public function insert_data($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert($this->main_table, $data);
		$insert_id = $this->db->getInsertId();
		return $insert_id;	
	}
	
	/**
	* 	update user
	* 	@param array $data 
	* 	@param array or string $where 
	* 	@return int affected count
	*/
	public function update_data($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table,$data,$where);
		return $result;
	}
	

	
	/**
	* 	delete user
	* 	@param array or string $where 
	* 	@return int affected count
	*/
	public function del_data($where){
		$result = $this->db->delete($this->main_table,$where);
		return $result;
	}
	/**
	* 	delete photos
	* 	@param string $photos 
	*/
	public function del_photo($photos){
		if($photos){
			$array = explode(',',$photos);
			foreach($array as $photo){
				$file = UPLOAD_PHOTO.$photo;
				$file2 = UPLOAD_PHOTO.'thumb/'.$photo;
				del_file($file);
				del_file($file2);
			}
		}
	}	
	/**
	* 	get user info
	* 	@param int $id 
	* 	@return array one row data
	*/

	public function get_data_row($id,$field='*'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$this->main_id."='".intval($id)."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}


        public function get_data_by_name($name,$field='admin_id,admin_name,admin_gid'){
                $sql = "SELECT ".$field." FROM ".$this->main_table." WHERE admin_name ='".trim($name)."' ";
		$data = $this->db->getRow($sql);
		return $data;

        }

        /**
	* 	get user array
	* 	@param array or string $where
	* 	@return array data
	*/
	public function get_data_array($where,$order='admin_id',$limit='10',$field='admin_id,admin_name,admin_check'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY ".$order;
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	}   
	

	/**
	* 	check user pwd 
	* 	@param int $id 
	*	@param string $password 
	* 	@return bool
	*/
	public function check_data_pwd($id,$password){
		$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE ".$this->main_id."='".intval($id)."' AND admin_pwd='".base64_encode(md5($password))."' ";
		$result = $this->db->getOne($sql);
		if ($result) {
			return true;
		} else {
			return false;
		}
	}
	

	
	/**
	* 	get group array
	* 	@return array 
	*/
	public function get_group_ary($type=''){
		global $cf_admin_info;
		$type = intval($type) ? intval($type) : $cf_admin_info['g_type'];
		switch($type){
		
			case "1":															//本权限组
				$sql = "SELECT g_id,g_level,g_title FROM sys_group WHERE g_id='".$cf_admin_info['admin_gid']."' ORDER BY g_root,g_nav ";
			break;
			case "2":															//指定权限组
				$sql = "SELECT g_id,g_level,g_title FROM sys_group WHERE LOCATE(concat(',',gid,','),'".$admin_info['g_groups']."')>0 ORDER BY g_root,g_nav ";
			break;
			case "3":															//所有
				$sql = "SELECT g_id,g_level,g_title FROM sys_group ORDER BY g_root,g_nav ";
			break;
			default:															//树形向下职能权限
				$sql = "SELECT g_id,g_level,g_title FROM sys_group WHERE LOCATE(concat(',',gid,','),'".$admin_info['g_groups']."')>0 ORDER BY g_root,g_nav ";
			break;
		
		}
		
		
		$data = $this->db->getAll($sql);
		if($data){
			$new_data = array();
			foreach($data as $k=>$v){
				$new_data[$v['g_id']] = str_repeat("├ ",$v['g_level']).$v['g_title'];	
			}
				
		}
		return $new_data;
	}

	/**
	* 	set news (hot display etc.)
	*	@param string $default 
	*	@param string $type 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function set_data($default='',$type,$where){
		if(!$type){
			my_jump('请指定更新类型');
		}
		$set = array();
		switch($type){
			case 'check':
				if($default=='1')
				$set = array('admin_check'=>'0');
				else
				$set = array('admin_check'=>'1');
			break;
			/*case 'bgadmin':
				if($default=='1')
				$set = array('admin_bgadmin'=>'0');
				else
				$set = array('admin_bgadmin'=>'1');
			break;*/
			default:
				my_jump('输入的参数有误');
			break;
		}
		$result = $this->db->update($this->main_table,$set,$where);
		return $result;
	}


	public function get_count($where){
		$sql = "SELECT count(*) as count FROM ".$this->main_table." WHERE ".$where;
		$data = $this->db->getOne($sql);
		return $data;
	}
	
	public function write_data(){
			$filename = 'localdata_admin.js';
			$varindex = 'admin_user';
			$sql = "SELECT admin_id,admin_name,admin_true_name,admin_phone,g_title,g_id,g_root,g_club_type FROM ".$this->main_table." LEFT JOIN sys_group on admin_gid=g_id WHERE admin_work_status<'2' order by admin_gid ";
			$data = $this->db->getAll($sql);

			if($data){
				$info = '';
				$write_info = "var ".$varindex." = [";
				foreach ($data as $item){
					$code = "\n".'{"key":"'.$item['admin_name'].'","id":"'.$item['admin_id'].'","gid":"'.$item['g_id'].'","club":"'.$item['g_club_type'].'","root":"'.$item['g_root'].'","keyname":"'.$item['admin_name'].'-'.$item['admin_true_name'].'","name":"'.$item['admin_true_name'].'","phone":"'.$item['admin_phone'].'","group":"'.$item['g_title'].'"}';
					$info .= $info ? ','.$code : $code;
				}	
				$write_info .=$info;	
				$write_info .= "\n];";
				$filename = INFO_PATH.$filename;
				write_file($filename,$write_info,'js');
			}
	}
	
	public function check_exist($name){
		$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE admin_name='".$name."'";
        $rs = $this->db->getOne($sql);
        return $rs;
	}
	
	public function save_item($gp_sell,$gp_uid,$phx_sell,$phx_uid,$kg_sell,$kg_uid){
		$array=array();
		if(is_array($gp_sell)){
			$gp = array();
			foreach($gp_sell as $k=>$v){
				if($v){
					//echo $k.':'.$v;
					$gp = array('user'=>$gp_sell[$k],'uid'=>$gp_uid[$k]);
					$array['GP'][] =$gp;
					}
			  
			}
		}
		//return $gp_sell;
		if(is_array($phx_sell)){
			$phx = array();
			foreach($phx_sell as $k=>$v){
				if($v){
					
					$phx = array('user'=>$phx_sell[$k],'uid'=>$phx_uid[$k]);
					$array['PHX'][] =$phx;
					}
			  
			}
		}
		
		if(is_array($kg_sell)){
			$kg = array();
			foreach($kg_sell as $k=>$v){
				if($v){
					$kg = array('user'=>$kg_sell[$k],'uid'=>$kg_uid[$k]);
					$array['KG'][] =$kg;
					}
			  
			}
		}
		$new_array = var_export($array,true);
		$input_data= "\$sell_array=".$new_array;
		write_file(INFO_PATH."sales_order.php",$input_data);
        return $array;
	}
	
	/**
	* 	get excel array
	* 	@return array 
	*/


	
}

?>